//
//  Manage2Controller.m
//  MY Project For Sport
//
//  Created by admin on 16/2/12.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "Manage2Controller.h"
#import "NetworkHandle.h"
#import "DataBaseHandle.h"
#import "ModelForManage2.h"
#import "Manage2OfCell.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667

@interface Manage2Controller ()<NetworkHandleDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, assign)BOOL isSave;
@property (nonatomic, retain)NSMutableArray *arrayForSave;
@property (nonatomic, retain)UIButton *button;
@end

@implementation Manage2Controller

-(void)dealloc {

    [_url release];
    [_tableView release];
    [_array release];
    [_arrayForSave release];
    [_model release];
    [_button release];
    [_name release];
    [super dealloc];

}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor redColor];
    [self handleData];
    [self createTableView];
    self.navigationItem.title = self.name;
    
    
}

- (void)handleData {

    [NetworkHandle handlerJSONWitURL:self.url delegate:self];
    
}

- (void)handleDidComplete:(id)result {

    self.array = [NSMutableArray array];
    
    NSDictionary *dic = [result objectForKey:@"result"];
    NSArray *array = [dic objectForKey:@"data"];
    
    for (NSDictionary *dicd in array) {
        
       ModelForManage2 *model = [[ModelForManage2 alloc] init];
        
        [model setValuesForKeysWithDictionary:dicd];
        
        [self.array addObject:model];
       // NSLog(@"%@", model.name_cn);
        //NSLog(@"%ld", self.array.count);
        [model release];
        
        
    }


    [self.tableView reloadData];

}

- (void)createTableView {


    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    [self.tableView registerClass:[Manage2OfCell class] forCellReuseIdentifier:@"poolForManage2"];
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return self.array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    Manage2OfCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForManage2"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ModelForManage2 *model1 = [self.array objectAtIndex:indexPath.row];
    
    [cell passModelForManage:model1];
    
    self.arrayForSave = [NSMutableArray array];
    self.arrayForSave = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
    for (ModelForManage2 *model in self.arrayForSave) {
 
        if ([model.name_cn isEqualToString:model1.name_cn]) {

        [cell.button setImage:[UIImage imageNamed:@"iconfont-guanzhu-2"] forState:UIControlStateNormal];

            self.isSave = YES;
            cell.save = self.isSave;
            break;
            
        }
        
        else {
            
            
            [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-guanzhu"] forState:UIControlStateNormal];
            
            self.isSave = NO;
            cell.save = self.isSave;
        }
       
    }
    
    return cell;
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.frame.size.height / 8;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
